package com.xbongbong.pay.platform.wechatpay.utils;

import com.alibaba.fastjson.JSONObject;
import com.wechat.pay.contrib.apache.httpclient.util.PemUtil;
import com.xbongbong.paas.toolbox.util.MD5Util;
import com.xbongbong.pay.utils.RsaUtil;

import java.io.ByteArrayInputStream;
import java.nio.charset.StandardCharsets;
import java.security.PrivateKey;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;

/**
 * 签名工具类
 * @author 魏荣杰
 * @date 2021/1/15 11:48
 * @since
 * @version
 */
public class WeChatSignUtil {

    public static String buildSignMessage(String appId, String timestamp, String nonceStr, String packages) {
        ArrayList<String> signMessage = new ArrayList<>();
        signMessage.add(appId);
        signMessage.add(timestamp);
        signMessage.add(nonceStr);
        signMessage.add(packages);
        return buildSignMessage(signMessage);
    }
    /**
     * Description: 构造签名串
     * @param method post,get, put
     * @param url 请求接口不带域名
     * @param timestamp 时间戳
     * @param nonceStr 随记字符串
     * @param body 请求报文主体
     * @return java.lang.String
     * @throws
     * @author 魏荣杰
     * @date 2021/1/15 11:52
     * @since
     */
    public static String buildSignMessage(String method, String url, long timestamp, String nonceStr, String body) {
        ArrayList<String> signMessage = new ArrayList<>();
        signMessage.add(method);
        signMessage.add(url);
        signMessage.add(String.valueOf(timestamp));
        signMessage.add(nonceStr);
        signMessage.add(body);
        return buildSignMessage(signMessage);
    }

    private static String buildSignMessage(ArrayList<String> signMessage) {
        if (signMessage == null || signMessage.size() <= 0) {
            return null;
        }
        StringBuffer stringBuffer = new StringBuffer();
        signMessage.forEach(v -> stringBuffer.append(v).append("\n"));
        return stringBuffer.toString();
    }

    /**
     * v3 接口创建签名
     *
     * @param signMessage 待签名的参数
     * @param privateKey     商户私钥
     * @return 生成 v3 签名
     * @throws Exception 异常信息
     */
    public static String createSign4V3(String signMessage, String privateKey) throws Exception {
        if (Objects.isNull(signMessage) || signMessage.length() == 0) {
            return null;
        }
        // 加载商户私钥（privateKey：私钥字符串）
        PrivateKey key = PemUtil
                .loadPrivateKey(new ByteArrayInputStream(privateKey.getBytes(StandardCharsets.UTF_8)));
        return RsaUtil.encryptByPrivateKey(signMessage, key);
    }

    public static String createSign4V3(String signMessage, PrivateKey privateKey) throws Exception {
        if (Objects.isNull(signMessage) || signMessage.length() == 0) {
            return null;
        }
        return RsaUtil.encryptByPrivateKey(signMessage, privateKey);
    }

    /**
     * Description: v2 接口创建签名
     * @param params
     * @param key
     * @return java.lang.String
     * @throws
     * @author 魏荣杰
     * @date 2021/1/18 15:01
     * @since
     */
    public static String createSign4V2(JSONObject jsonObject, String key) {
        Map<String, String> params = new HashMap<>();
        jsonObject.forEach((k, v) -> params.put(k, v.toString()));
        String linkString = WeChatPayHelp.createLinkString(params, "&", false, false);
        String stringSignTemp = linkString + "&key=" + key;
        return MD5Util.md5(stringSignTemp).toUpperCase();
    }
}
